#include "StdAfx.h"
#include <cmath>
#include "constant_side.h"
#include "tools.h"


constant_side::constant_side(void)
{
	rise = 0.0f;
	depth = 0.1f;
	width_percent = 1.0f;
}


constant_side::~constant_side(void)
{
}

void constant_side::draw(float* start, float* end, float start_dist, float end_dist, float* delta)
{

	float delta_length = sqrt(delta[0]*delta[0] + delta[2]*delta[2]);
	float right_side_normal[3] = {delta[0]/delta_length, 0.0f, delta[2]/delta_length};
	float left_side_normal[3] = {-right_side_normal[0], 0.0f, -right_side_normal[2]};
	
	float start_phrase = phrase + start_dist/cycle;
	float end_phrase = phrase + end_dist/cycle;

	glBindTexture(GL_TEXTURE_2D, texture);

	glBegin(GL_QUADS);
	//left
	glNormal3fv(left_side_normal);
	glTexCoord2f(start_phrase, 1.0f);
	glVertex3f(start[0] - width_percent*delta[0], start[1] + rise, start[2] - width_percent*delta[2]);

	glNormal3fv(left_side_normal);
	glTexCoord2f(start_phrase, 0.0f);
	glVertex3f(start[0] - width_percent*delta[0], start[1] + rise - depth, start[2] - width_percent*delta[2]);

	glNormal3fv(left_side_normal);
	glTexCoord2f(end_phrase, 0.0f);
	glVertex3f(end[0] - width_percent*delta[0], end[1] + rise - depth, end[2] - width_percent*delta[2]);

	glNormal3fv(left_side_normal);
	glTexCoord2f(end_phrase, 1.0f);
	glVertex3f(end[0] - width_percent*delta[0], end[1] + rise, end[2] - width_percent*delta[2]);


	//right
	glNormal3fv(right_side_normal);
	glTexCoord2f(start_phrase, 1.0f);
	glVertex3f(start[0] + width_percent*delta[0], start[1] + rise, start[2] + width_percent*delta[2]);

	glNormal3fv(right_side_normal);
	glTexCoord2f(end_phrase, 1.0f);
	glVertex3f(end[0] + width_percent*delta[0], end[1] + rise, end[2] + width_percent*delta[2]);

	glNormal3fv(right_side_normal);
	glTexCoord2f(end_phrase, 0.0f);
	glVertex3f(end[0] + width_percent*delta[0], end[1] + rise - depth, end[2] + width_percent*delta[2]);

	glNormal3fv(right_side_normal);
	glTexCoord2f(start_phrase, 0.0f);
	glVertex3f(start[0] + width_percent*delta[0], start[1] + rise - depth, start[2] + width_percent*delta[2]);

	glEnd();
}

void constant_side::draw_corner(float *previous_point, float *cur_point, 
									float* previous_delta, float *cur_delta,
									float* center_point, float corner_radius)
{
	float right_side_normal[3] = { previous_delta[0] + cur_delta[0], 
									previous_delta[1] + cur_delta[1],
									previous_delta[2] + cur_delta[2]};
	nomalize(right_side_normal);
	float left_side_normal[3] = { -right_side_normal[0], right_side_normal[1], -right_side_normal[2]};

	float corner_phrase_begin = phrase;
	float corner_phrase_end = phrase + corner_radius*2/cycle;
	float corner_phrase_center = (corner_phrase_begin + corner_phrase_end)/2;

	glBindTexture(GL_TEXTURE_2D, texture);

	glBegin(GL_QUADS);
	//left
	glNormal3fv(left_side_normal);
	glTexCoord2f(corner_phrase_begin, 1.0f);
	glVertex3f(previous_point[0] - width_percent*previous_delta[0], 
				previous_point[1] - width_percent*previous_delta[1] + rise, 
				previous_point[2] - width_percent*previous_delta[2]);

	glNormal3fv(left_side_normal);
	glTexCoord2f(corner_phrase_begin, 0.0f);
	glVertex3f(previous_point[0] - width_percent*previous_delta[0], 
		previous_point[1] - width_percent*previous_delta[1] + rise - depth, 
		previous_point[2] - width_percent*previous_delta[2]);

	glNormal3fv(left_side_normal);
	glTexCoord2f(corner_phrase_end, 0.0f);
	glVertex3f(cur_point[0] - width_percent*cur_delta[0], 
		cur_point[1] - width_percent*cur_delta[1] + rise - depth, 
		cur_point[2] - width_percent*cur_delta[2]);

	glNormal3fv(left_side_normal);
	glTexCoord2f(corner_phrase_end, 1.0f);
	glVertex3f(cur_point[0] - width_percent*cur_delta[0], 
				cur_point[1] - width_percent*cur_delta[1]+ rise, 
				cur_point[2] - width_percent*cur_delta[2]);

	//right
	glNormal3fv(right_side_normal);
	glTexCoord2f(corner_phrase_begin, 1.0f);
	glVertex3f(previous_point[0] + width_percent*previous_delta[0], 
		previous_point[1] + width_percent*previous_delta[1] + rise, 
		previous_point[2] + width_percent*previous_delta[2]);

	glNormal3fv(right_side_normal);
	glTexCoord2f(corner_phrase_end, 1.0f);
	glVertex3f(cur_point[0] + width_percent*cur_delta[0], 
				cur_point[1] + width_percent*cur_delta[1] + rise, 
				cur_point[2] + width_percent*cur_delta[2]);

	glNormal3fv(right_side_normal);
	glTexCoord2f(corner_phrase_end, 0.0f);
	glVertex3f(cur_point[0] + width_percent*cur_delta[0], 
		cur_point[1] + width_percent*cur_delta[1] + rise - depth, 
		cur_point[2] + width_percent*cur_delta[2]);

	glNormal3fv(right_side_normal);
	glTexCoord2f(corner_phrase_begin, 0.0f);
	glVertex3f(previous_point[0] + width_percent*previous_delta[0], 
		previous_point[1] + width_percent*previous_delta[1] + rise - depth, 
		previous_point[2] + width_percent*previous_delta[2]);
	
	glEnd();

}